<template>
  <div class="container">
    <!-- 登录面板 -->
    <div class="login-container">
      <div class="login-logo-container">
        <img alt="" class="login-logo" src="../assets/logo.jpg"/>
      </div>

      <!-- 系统名称 -->
      <div class="login-title">一麓童行-后台管理系统</div>
      <!-- 登录表单区域 -->
      <keep-alive>
        <div class="login-form-container">
          <el-form ref="form" :model="form" label-width="60px">
            <el-form-item label="用户名">
              <el-input v-model="form.username"></el-input>
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password"
                        show-password
                        style="width: 100%"
              />
              <!--            <el-input v-model="form.password" show-password></el-input>-->
            </el-form-item>
            <div class="login-btn">
              <el-button type="primary" @click="isShow=true">登录</el-button>
              <el-button type="danger" @click="handleClear">取消</el-button>
            </div>
          </el-form>
        </div>
      </keep-alive>
    </div>
    <Vcode :show="isShow"
           @success="handleLogin(form)"

    />
  </div>
</template>

<script>
import Vcode from "vue-puzzle-vcode";

export default {
  components: {
    Vcode,
  },
  data () {
    return {
      // 登录表单
      isShow: false,
      form: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    // 登录函数
    async handleLogin (form) {
      // console.log('登录')
      this.isShow = false;
      if (form.username === "") {
        this.$message.warning("请输入用户名");
        return;
      }
      if (form.password === "") {
        this.$message.warning("请输入密码");
        return;
      }
      const params = new URLSearchParams();
      params.append("username", this.form.username);
      params.append("password", this.form.password);
      params.append("usertype", "admin");
      const res = await this.$http.login(params);

      if (res.code !== 0) {
        this.$message.error(res.msg);
      } else {
        sessionStorage.setItem("token", res.token);
        sessionStorage.setItem("info", res.info);
        await this.$router.push("/home");
      }
    },

    // 取消函数
    handleClear () {
      this.form = {
        username: "",
        password: "",
      };
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100vh;
  width: 100vw;
  background-color: #00a0e9;
  display: flex;
  flex-direction: column;
  align-content: center;
  justify-content: center;

  .login-container {
    width: 80%;
    height: 400px;
    margin: 0 auto;
    border-radius: 2em;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.26);
    background-color: white;

    .login-logo-container {
      width: 120px;
      height: 120px;
      background-color: white;
      margin: 0 auto;
      transform: translate(0, -50%);
      border-radius: 50%;

      .login-logo {
        border-radius: 50%;
        width: 120px;
        height: 120px;
        overflow: hidden;
        display: block;
        margin: 0 auto;
      }
    }

    .login-title {
      font-size: 1.8em;
      text-align: center;
      transform: translate(0, -40px);
      cursor: pointer;
      user-select: none;
    }

    .login-form-container {
      margin: 0 100px 0 60px;
    }

    .login-btn {
      display: flex;
      align-content: center;
      justify-content: center;
    }
  }

  @media screen and (min-width: 900px) {
    .login-container {
      width: 600px;
      height: 400px;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.26);
      background-color: white;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);

      .login-logo-container {
        width: 120px;
        height: 120px;
        padding: 10px;
        background-color: white;
        margin: 0 auto;
        transform: translate(0, -50%);
        border-radius: 50%;

        .login-logo {
          border-radius: 50%;
          width: 120px;
          height: 120px;
          overflow: hidden;
          display: block;
          margin: 0 auto;
          cursor: pointer;
          user-select: none;
        }
      }

      .login-title {
        font-size: 1.8em;
        text-align: center;
        transform: translate(0, -40px);
        cursor: pointer;
        user-select: none;
      }

      .login-form-container {
        margin: 0 100px 0 60px;
      }
    }


  }
}
</style>
